<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://unpkg.com/vue@2.7.16/dist/vue.js"></script>
    <script src="https://unpkg.com/element-ui@2.15.6/lib/index.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui@2.15.6/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <!--搜索表单-->
    <el-form :inline="true" :model="brand" class="demo-form-inline">
        <el-form-item label="当前状态">
            <el-select v-model="search.status" placeholder="当前状态">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="企业名称">
            <el-input v-model="search.companyName" placeholder="企业名称"></el-input>
        </el-form-item>
        <el-form-item label="品牌名称">
            <el-input v-model="search.brandName" placeholder="品牌名称"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>
    <!--按钮-->
    <el-row>
        <el-button type="danger" plain @click="deleteByIds">批量删除</el-button>
        <el-button type="primary" plain @click="addDialog">新增</el-button>
    </el-row>
    <!--添加数据对话框表单-->
    <el-dialog
            :title="title"
            :visible.sync="dialogVisible"
            width="50%">
        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch v-model="brand.status"
                           :active-value="1"
                           :inactive-value="0"
                ></el-switch>
            </el-form-item>
            <el-form-item>
                <el-button v-if="title === '增加品牌'" type="primary" @click="addBrand">提交</el-button>
                <el-button v-else type="primary" @click="updateBrand">更新</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>
            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    align="center"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="status"
                    align="center"
                    label="当前状态">
            </el-table-column>
            <el-table-column align="center" label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" @click="updateDialog(scope.row.id)">修改</el-button>
                    <el-button type="danger" @click="deleteById(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </template>
    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>
</div>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                // 当前页码
                currentPage: 1,
                pageSize: 5,
                totalCount: 10,
                // 添加数据对话框是否展示的标记
                dialogVisible: false,
                // 品牌模型数据
                brand: {
                    status: 0,
                    brandName: '',
                    companyName: '',
                    id: '',
                    ordered: '',
                    description: ''
                },
                // 搜索表单
                search:{
                    status: '',
                    brandName: '',
                    companyName: '',
                    id: '',
                    ordered: '',
                    description: ''
                },
                // 复选框选中数据集合
                multipleSelection: [],
                // 表格数据
                tableData: [],
                selectedIds: [],
                // 对话框标题
                title: '',
                // 当前行的id
                id: '',
            }
        },
        mounted() {
            this.selectAll();
        },
        methods: {
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            // 复选框选中后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 分页
            handleSizeChange(val) {
                // 重新设置每页显示的条数
                this.pageSize = val;
                //调用 selectAll 函数重新分页查询数据
                this.selectAll();
            },
            handleCurrentChange(val) {
                // 重新设置当前页码
                this.currentPage = val;
                //调用 selectAll 函数重新分页查询数据
                this.selectAll();
            },
            // 查询所有数据
            selectAll() {
                axios.get("http://localhost:8080/LoginAndRegistration/brand/selectByPage?currentPage=" + this.currentPage + "&pageSize=" + this.pageSize).then((resp) => {
                    this.tableData = resp.data.rows;
                    this.totalCount = resp.data.totalCount;
                })
            },
            // 查询
            onSubmit() {
                axios.post("http://localhost:8080/LoginAndRegistration/brand/selectByPageAndCondition?currentPage=" + this.currentPage + "&pageSize=" + this.pageSize, this.search).then((resp) => {
                    this.tableData = resp.data.rows;
                    this.totalCount = resp.data.totalCount;
                })
            },
            // 添加数据
            addBrand() {
                axios.post("http://localhost:8080/LoginAndRegistration/brand/add", this.brand).then((resp) => {
                    if (resp.data == "success") {
                        this.dialogVisible = false;
                        this.selectAll();
                        this.$message({
                            message: '恭喜你，添加成功',
                            type: 'success'
                        });
                    }
                })
            },
            // 批量删除数据
            deleteByIds() {
                this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        let selectionElement = this.multipleSelection[i];
                        this.selectedIds[i] = selectionElement.id;
                    }
                    axios.post("http://localhost:8080/LoginAndRegistration/brand/deleteByIds", this.selectedIds).then((resp) => {
                        if (resp.data == "success") {
                            this.selectAll();
                            this.$message({
                                message: '恭喜你，删除成功',
                                type: 'success'
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            // 根据id删除数据
            deleteById(id) {
                this.$confirm('确认删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post("http://localhost:8080/LoginAndRegistration/brand/deleteById", id).then((resp) => {
                        if (resp.data == "success") {
                            this.selectAll();
                            this.$message({
                                message: '恭喜你，删除成功',
                                type: 'success'
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            addDialog() {
                this.dialogVisible = true;
                this.title = "增加品牌";
            },
            updateDialog(id) {
                this.dialogVisible = true;
                this.title = "修改品牌";
                axios.get("http://localhost:8080/LoginAndRegistration/brand/selectById?id="+id).then((resp) => {
                    this.brand.id = id;
                    this.brand.brandName = resp.data.brandName;
                    this.brand.companyName = resp.data.companyName;
                    this.brand.ordered = resp.data.ordered;
                    this.brand.description = resp.data.description;
                    this.brand.status = resp.data.status;
                })
            },
            updateBrand() {
                axios.post("http://localhost:8080/LoginAndRegistration/brand/updateBrand",this.brand).then((resp) => {
                    if (resp.data == "success") {
                        this.dialogVisible = false;
                        this.selectAll();
                        this.$message({
                            message: '恭喜你，修改成功',
                            type: 'success'
                        });
                    }
                })
            }
        },
    })
</script>
</body>
</html>